package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsSeckillGoods;
import com.example.xpvideo.entity.LsTeamActivity;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.SecckillGoodsReq;
import com.example.xpvideo.entity.req.TeamGoodReq;
import com.example.xpvideo.entity.res.SeckillGoodsRes;
import com.example.xpvideo.entity.res.TeamGoodRes;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.TeamGoodsMapper;
import com.example.xpvideo.service.TeamGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.util.List;

@Service
public class TeamGoodsSeviceImpl implements TeamGoodsService {
    @Autowired
    private TeamGoodsMapper teamGoodsMapper;

    @Override
    public ResponseResult<PageResult<List<TeamGoodRes>>> list(TeamGoodReq teamGoodReq) throws ErrorException {
        String name = teamGoodReq.getName();
        LocalDate startDate = teamGoodReq.getStartDate();
        LocalDate endDate = teamGoodReq.getEndDate();
        String startTime = teamGoodReq.getStartTime();
        String endTime = teamGoodReq.getEndTime();
        Integer shopId = teamGoodReq.getShopId();
        String shopName = teamGoodReq.getShopName();

        int page = PageContext.getPage();
        int size = PageContext.getSize();

        PageHelper.startPage(page, size);

        List<TeamGoodRes> list = teamGoodsMapper.TeamGoodsList(Wrappers.<LsTeamActivity>query()
                .like(!ObjectUtils.isEmpty(name),"name",name)
                .ge(!ObjectUtils.isEmpty(startDate),"start_date",startDate)
                .le(!ObjectUtils.isEmpty(endDate),"end_date",endDate)
                .ge(!ObjectUtils.isEmpty(startTime),"start_time",startTime)
                .le(!ObjectUtils.isEmpty(endTime),"end_time",endTime)
                .eq("del",0));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<TeamGoodRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }
}
